package com.ktjy.mapper.user;

import com.ktjy.pojo.SysUser;
import com.ktjy.utils.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;

import javax.xml.crypto.Data;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SysUserMapperTest {
    private Logger logger = Logger.getLogger(SysUserMapperTest.class);
    @Test
    public  void count() throws Exception{
        SqlSession sqlSession = null;
        try{
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession();
            int count = sqlSession.getMapper(SysUserMapper.class).count();
            logger.debug("SysUserMapperTest count --->"+count);
        }finally {
            sqlSession.close();
        }
    }
    public void testUserListQuery() throws Exception{
        SqlSession sqlSession = null;
        List<SysUser> userList =null;
        try{
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession();
            userList = sqlSession.getMapper(SysUserMapper.class).getUserList();
        }finally {
            sqlSession.close();
        }
        if(userList !=null){
            for(SysUser user : userList){
                logger.debug("testUserListQuery account:"+user.getAccount()+"and realName:"+user.getRealName());
            }
        }
    }

    private SqlSession sqlSession;
    List<SysUser> userList = sqlSession.getMapper(SysUserMapper.class)
            .getUsersByRealName("李");

    SysUser user = new SysUser();
    user.setRealName("李");
    user.setRoleId(3);
    List<SysUser> userList =
            sqlSession.getMapper(SysUserMapper.class).getUserListByPojo(user);


    Map<String,Object> userMap = new HashMap<String,Object>();
    userMap.put("rName","赵");
    userMap.put("rId",2);
    List<SysUser> userList =
            sqlSession.getMapper(SysUserMapper.class).getUserListByMap(userMap);

    List<SysUser> userList =
            sqlSession.getMapper(SysUserMapper.class).getUserListByParams("赵",2);

    List<SysUser> userList =
            sqlSession.getMapper(SysUserMapper.class).getUserListByRoleId(2);
    for(SysUser user:userList){
        logger.debug("testGetUserList userId:" + user.getId()+
                "and realName:"+user.getRealName()+
                "and roleId:"+user.getSysRole().getId()+
                "and code:" + user.getSysRole().getCode() +
                "and roleName:" + user.getSysRole().getRoleName());
    }

    List<SysUser> userList = sqlSession.getMapper(SysUserMapper.class)
            .getUserAndAddressesByUserId(1);

    for(SysUser user:userList){
        logger.debug("userList:(include:addresslist)====> account:"
            + user.getAccount()+",realName:"+user.getRealName());
        for(Address address :user.getAddressList()){
            logger.debug("address ----> id:"+address.getId()
            +",contact:" + address.getCountact()
            + ",addressDesc:" + address.getAddressDesc()
            + ",tel:"+address.getTel()
            + ",postCode:" + address.getPostCode());
        }
    }
    public void testAdd(){
        logger.debug("testAdd !=================");
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            SysUser user = new SysUser();
            user.setAccount("test001");
            user.setRealName("测试用户001");
            user.setPassword("123456");
            Data birthday = new SimpleDateFormat("yyyy-MM-dd").parse("2023-10-22");
            user.setBirthday(birthday);
            user.setAddress("测试地址abc");
            user.setSex(1);
            user.setPhone("13411110000");
            user.setRoleId(1);
            user.setCreatedUserId(1);
            user.setCreatedTime(new Date());
            count  = sqlSession.getMapper(SysUserMapper.class).add(user);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:"+count);
    }

    public void testUpdatePwd(){
        logger.debug("testUpdatePwd !=============");
        SqlSession sqlSession = null;
        String pwd = "88888888";
        Integer id = 17;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(SysUserMapper.class).updatePwd(id,pwd);
            sqlSession.close();
        }catch (Exception e){
            e.printStackTrace();;
            sqlSession.rollback();
            count = 0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testUpdatePwd count:"+count);
    }

}
